package org.hongqi.common.senpec.sys.department;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hongqi.common.senpec.base.mybatisplus.IBaseEntity;
import org.hongqi.common.senpec.base.mybatisplus.IBaseServiceImpl;
import org.hongqi.common.senpec.base.sevlet.Result;
import org.hongqi.common.senpec.base.spring.redis.RedisService;
import org.hongqi.common.senpec.base.utils.ObjectsUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class DepartmentServiceImpl extends IBaseServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    private final DepartmentMapper departmentMapper;
    private final RedisService<Department> redisService;
    private final String redisKey = "sys_department_list";

    @Override
    public Result<Department> mySave(Department entity) {
        int num = departmentMapper.insert(entity);
        if (num == 0) {
            return Result.error(entity);
        } else {
            redisService.deleteKey(redisKey);
            return Result.ok(entity);
        }
    }

    @Override
    public Result<?> myRemoveById(Long id) {
        int num = departmentMapper.deleteById(id);
        if (num == 0) {
            return Result.error();
        } else {
            redisService.deleteKey(redisKey);
            return Result.ok();
        }
    }

    @Override
    public Result<Department> myUpdateById(Department entity) {
        int num = departmentMapper.updateById(entity);
        if (num == 0) {
            return Result.error(entity);
        } else {
            redisService.deleteKey(redisKey);
            return Result.ok(departmentMapper.selectById(entity.getId()));
        }
    }

    @Override
    public Result<Department> myGetById(Long id) {
        Department department = departmentMapper.selectById(id);
        return ObjectsUtil.isNull(department) ? Result.error(new Department()) : Result.ok(department);
    }

    @Override
    public Result<Collection<Department>> myListTree(Department entity) {
        Collection<Department> departments = departmentMapper.selectList(null);
        departments = departments.stream().filter(Department::getIsRoot).toList();
        departments.forEach(department -> department.setChildren(myListByPid(department.getId())));
        return Result.ok(departments);
    }

    @Override
    public Collection<Department> myListByPid(Long pid) {
        Collection<Department> departments = departmentMapper.selectList(null);
        departments = departments.stream().filter(department -> Objects.equals(department.getParent(), pid)).toList();
        departments.forEach(department -> department.setChildren(myListByPid(department.getId())));
        return departments;
    }

    @Override
    public List<Long> myListChildrenIdsByPid(Long pid) {
        Collection<Department> departments = departmentMapper.selectList(null);
        List<Long> idList = departments.stream().filter(department -> Objects.equals(department.getParent(), pid)).map(IBaseEntity::getId).toList();
        List<Long> ids = new ArrayList<>(idList);
        ids.add(pid);
        idList.forEach(id -> ids.addAll(myListChildrenIdsByPid(id)));
        return ids;
    }

}
